//
//  HX_AddMailList.m
//  UI17 - Mail-List
//
//  Created by 韩旭 on 16/8/6.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "HX_AddMailList.h"
#import "MailModel.h"
#import "HX_SQLiteManger.h"

@interface HX_AddMailList ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, strong) UITextField *fieldName;
@property (nonatomic, strong) NSData *data;
@property (nonatomic, strong) UITextField *fieldAdress;
@property (nonatomic, strong) UITextField *fieldTelePhone;
@property (nonatomic, strong) UITextField *fieldGender;

@end

@implementation HX_AddMailList
- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = COLORAUTO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(backGround:)];
    [self creatFieldText];
}
- (void)backGround:(UINavigationBar *)bar {
    if (_fieldName.text.length > 0) {
        MailModel *model = [[MailModel alloc] init];
        model.name = _fieldName.text;
        NSString *string = @"datasImage";
        NSString *path  = [DATA stringByAppendingPathComponent:string];
        int result =  [_data writeToFile:path atomically:YES];
        if (result) {
            NSLog(@"写入成功");
        } else {
            NSLog(@"没有");
        }
        model.pathImage = string;
        [self.delegate sandToModle:model];
    }
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)creatFieldText {
    _fieldName = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, kWidth, 100)];
    _fieldName.backgroundColor = COLORAUTO;
    [self.view addSubview:_fieldName];
    _fieldGender = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, kWidth, 100)];
    _fieldGender.backgroundColor = COLORAUTO;
    [self.view addSubview:_fieldGender];
    _fieldAdress = [[UITextField alloc] initWithFrame:CGRectMake(0, 200, kWidth, 100)];
    _fieldAdress.backgroundColor = COLORAUTO;
    [self.view addSubview:_fieldAdress];
    
    _fieldTelePhone = [[UITextField alloc] initWithFrame:CGRectMake(0, 300, kWidth, 100)];
    _fieldTelePhone.backgroundColor = COLORAUTO;
    [self.view addSubview:_fieldTelePhone];
    _imageHead = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth / 2, 200)];
    _imageHead.center = CGPointMake(kWidth / 2, kHeight * 0.7);
    _imageHead.backgroundColor = [UIColor redColor];
    [self.view addSubview:_imageHead];
    _imageHead.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getImageFromIpc)];
    [_imageHead addGestureRecognizer:tap];
}
- (void)getImageFromIpc {
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    ipc.delegate = self;
    [self presentViewController:ipc animated:YES completion:^{
    }];
}
#pragma mark --<UIImagePickerControllerDelegate>--
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    self.imageHead.image = info[UIImagePickerControllerOriginalImage];
    self.data = UIImageJPEGRepresentation(self.imageHead.image, 0.1);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
